package com.squirrel.michale;

import java.util.ArrayList;
import java.util.List;

/**
 * TODO function description
 *
 * @author m30026548
 * @since 2025-04-07
 */
public class Router {

    private boolean addMode = true;

    private int memoryLimit;

    private int count = 0;

    private List<DataPackage> dataPackageList = new ArrayList<DataPackage>();

    public Router() {
    }

    public Router(int memoryLimit) {
        this.memoryLimit = memoryLimit;

    }


    public boolean addPacket(int source, int destination, int timestamp) {

        if (!addMode) {
            addMode = true;
        }

        for (int i = 0; i < dataPackageList.size(); i++) {
            DataPackage dataPackage = dataPackageList.get(i);
            if (dataPackage.getSource() == source && dataPackage.getDestination() == destination && dataPackage.getTimestamp() == timestamp) {
                return false;
            }
        }


        if (dataPackageList.size() < memoryLimit) {
            dataPackageList.add(new DataPackage(source, destination, timestamp));
            count++;
            return true;
        }else if (dataPackageList.size() == memoryLimit) {
            int oldest = 0;
            for (int i = 1; i < dataPackageList.size(); i++) {
                DataPackage dataPackage = dataPackageList.get(i);
                if (dataPackage.getTimestamp()<dataPackageList.get(oldest).getTimestamp()) {
                    oldest = i;
                }
            }
            dataPackageList.remove(oldest);
            dataPackageList.add(new DataPackage(source, destination, timestamp));
            return true;
        }
        return false;
    }


    public int[] forwardPacket(){
        if (addMode) {
            addMode = false;
        }
        if (!addMode) {
            if (dataPackageList.size() >0) {
                DataPackage dataPackage = dataPackageList.get(0);
                dataPackageList.remove(0);
                count--;
                return new int[]{dataPackage.getSource(), dataPackage.getDestination(), dataPackage.getTimestamp()};
            }else {
                return new int[]{};
            }
        }
        return new int[]{};

    }



    public int getCount(int destination, int startTime, int endTime){
        int count = 0;
        for (int i = 0; i < dataPackageList.size(); i++) {
            DataPackage dataPackage = dataPackageList.get(i);
            if (dataPackage.getDestination() == destination && dataPackage.getTimestamp() >= startTime && dataPackage.getTimestamp() <= endTime) {
                count++;
            }
        }
        return count;
    }


    private int lowerBound(List<Integer> nums, int target, int left) {
        int right = nums.size();
        while (left + 1 < right) {
            int mid = (left + right) >>> 1;
            if (nums.get(mid) >= target) {
                right = mid;
            } else {
                left = mid;
            }
        }
        return right;
    }

//    作者：灵茶山艾府
//    链接：https://leetcode.cn/problems/implement-router/solutions/3641772/mo-ni-ha-xi-biao-dui-lie-er-fen-cha-zhao-y7l7/
//    来源：力扣（LeetCode）
//    著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。



}
